import "babel-polyfill"; //兼容IE浏览器
import Vue from 'vue'
import App from './App.vue'
//引入elementUI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//引入VueRouter
import VueRouter from 'vue-router'
import Vuex from 'vuex'
import store from './vuex/store.js'
//引用routers.js
import routes from './routes'
//引入VueResource
import VueResource from 'vue-resource'
//引入字符图标
import '../icon/iconfont.css'
//引入富文本编辑器
import VueQuillEditor from 'vue-quill-editor'

//引用自定义组件（树形表格）
import TreeTable from './components/treeTable'

Vue.use(Vuex)
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(VueResource)
Vue.use(TreeTable)
Vue.use(VueQuillEditor)

//创建一个router实例
const router = new VueRouter({
	routes
})

let menus = JSON.parse(sessionStorage.getItem('menus'))
if (menus) {  
	store.commit('addMenu',menus)  
	router.addRoutes(store.state.items)  
}  

router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    sessionStorage.removeItem('user');
    sessionStorage.removeItem('provincialCity');
//  sessionStorage.removeItem('manufactures');
    sessionStorage.removeItem('menus');
    store.commit('addMenu',[]);
  }
  
  let user = JSON.parse(sessionStorage.getItem('user'));
  if (!user && to.path !== '/') {
    next({ path: '/' })
  } else {
    next()
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
